<template>
  <div class="section-wrap container">
    <div class="section-title">确认订单</div>
    <el-row type="flex" align="middle" justify="space-between" class="section-tab">
      <span>课程详情</span>
      <span>售价</span>
    </el-row>
    <div class="section-buy" v-if="!$util.isEmpty(orderData)">
      <div class="pay-coupons">
        <div class="pay-coupons-list">
          <el-row type="flex" justify="space-between" class="item">
            <el-tag class="type exan" effect="plain" size="mini">考试</el-tag>
            <el-row type="flex" justify="space-between" class="col">
              <div class="name">{{ orderData.title }}</div>
            </el-row>
            <div class="pirce">{{ orderData.user_price | formatMoney }}</div>
          </el-row>
        </div>
      </div>
      <!-- 积分抵扣 -->
      <Integral
        :credit_arr="credit_arr"
        :value="credit_price"
        :credit_redeem="credit_redeem"
        @change="changePoints"
      ></Integral>
      <el-divider></el-divider>
      <!-- 付款信息 -->
      <el-row class="pay-buy" type="flex" align="middle" justify="end">
        <el-row type="flex" justify="space-between" class="money">
          <el-row type="flex" align="middle" justify="space-between">
            <div class="text">合计</div>
            <div class="price">{{ total_price | formatMoney }}</div>
          </el-row>
        </el-row>
        <el-button type="primary" @click="handleSubmitOrder" :disabled="disabled"
          >提交订单</el-button
        >
      </el-row>
    </div>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import Integral from './components/integral'
export default {
  components: {
    Integral
  },
  data() {
    return {
      orderData: {},
      disabled: false,
      total_price: 0,
      credit_redeem: 0, // 是否开启积分抵扣
      credit_arr: [],
      credit_price: 0,
      creditObj: {}
    }
  },
  created() {
    this.handleGetOrderData()
  },
  methods: {
    ...mapMutations({
      changeOrderData: 'order/SET_ORDER_DATA'
    }),
    // 获取订单信息
    handleGetOrderData() {
      const _this = this
      const calc = {
        // 公开考试
        paper: function() {
          _this.$api.getOrderExamPaper(
            { id: _this.$route.query.id, paper_id: _this.$route.query.paperId },
            res => {
              calc.data(res.data)
            }
          )
        },
        // 专项
        special: function() {
          _this.$api.getOrderExamSpecial({ special_id: _this.$route.query.id }, res => {
            calc.data(res.data)
          })
        },
        // 套卷
        rollup: function() {
          _this.$api.getOrderExamRollup({ rollup_id: _this.$route.query.id }, res => {
            calc.data(res.data)
          })
        },
        run: function(fn) {
          return fn && fn()
        },
        data: function(data) {
          _this.orderData = data
          _this.total_price = data.user_price
          _this.credit_arr = data.credit_arr
          _this.credit_redeem = data.credit_redeem
          _this.changePoints(data.credit_arr[0])
        }
      }
      calc.run(calc[_this.$route.query.type])
    },
    // 提交订单
    handleSubmitOrder() {
      const _this = this
      _this.disabled = true
      let query = {}
      if (!this.$util.isEmpty(_this.creditObj)) {
        query = {
          credit: JSON.stringify(_this.creditObj)
        }
      }
      const calc = {
        // 公开考试
        paper: function() {
          _this.$api.createOrderExamPaper(
            { ...query, id: _this.$route.query.id, paper_id: _this.$route.query.paperId },
            res => {
              calc.order(res)
            },
            error => {
              calc.error(error.msg)
            }
          )
        },
        // 专项
        special: function() {
          _this.$api.createOrderExamSpecial(
            { special_id: _this.$route.query.id, ...query },
            res => {
              calc.order(res)
            },
            error => {
              calc.error(error.msg)
            }
          )
        },
        // 套卷
        rollup: function() {
          _this.$api.createOrderExamRollup(
            { rollup_id: _this.$route.query.id, ...query },
            res => {
              calc.order(res)
            },
            error => {
              calc.error(error.msg)
            }
          )
        },
        order: function(res) {
          const { data } = res
          if (data.need_pay) {
            _this.$router.push({
              path: '/buy/confirm'
            })
            _this.changeOrderData({
              order_no: data.order_no,
              payment: data.payment,
              return_url: '/exam'
            })
          } else {
            _this.$message.success(res.msg)
            _this.$router.replace({
              path: '/exam'
            })
          }
        },
        error: function(msg) {
          _this.$message.error(msg)
          _this.disabled = false
        },
        run: function(fn) {
          return fn && fn()
        }
      }
      calc.run(calc[_this.$route.query.type])
    },
    // 积分抵扣
    changePoints(obj) {
      let credit = 0
      if (!this.$util.isEmpty(obj)) {
        credit = obj.num
      }
      this.creditObj = obj
      this.credit_price = credit
      this.total_price = this.orderData.user_price - credit
    }
  }
}
</script>

<style lang="scss">
.section-wrap {
  margin-top: 10px;
  margin-bottom: 20px;
  padding-bottom: 40px;
  background-color: #ffffff;
  .section-title {
    padding-left: 30px;
    height: 44px;
    font-size: 16px;
    font-weight: 500;
    color: $text-color-import;
    line-height: 44px;
  }
  .section-tab {
    padding: 0 56px 0 210px;
    margin: 0 6px;
    height: 44px;
    font-size: 14px;
    font-weight: 500;
    color: $text-color-secondary;
    background: rgba(246, 249, 255, 1);
  }
}
.section-buy {
  .pay-coupons {
    margin-top: 20px;
    padding: 0 30px;
    .pay-coupons-list {
      margin-top: 10px;
      padding: 2px 20px 22px;
      border: 1px solid rgba(235, 238, 245, 1);
      .item {
        margin-top: 20px;
        .type {
          border-radius: 0;
          border-top-left-radius: 8px;
          border-bottom-right-radius: 8px;
          &.exam {
            border-color: #4bd7ec;
            color: #4bd7ec;
          }
        }
        .col {
          flex: 1;
          flex-direction: column;
          margin-left: 10px;
          padding-bottom: 15px;
        }
        .name {
          font-size: 13px;
          font-weight: 500;
          color: $text-color-import;
          line-height: 18px;
          margin-bottom: 20px;
        }
        .count {
          font-size: 12px;
          color: $text-color-secondary;
        }
        .pirce {
          font-size: 14px;
          font-weight: 500;
          color: $text-color-price;
        }
      }
    }
  }
  .pay-buy {
    padding: 0 30px;
    .money {
      flex-direction: column;
      width: 130px;
      margin-right: 40px;
      .text {
        font-size: 13px;
        font-weight: 400;
        color: $text-color-secondary;
      }
      .price {
        font-size: 18px;
        font-weight: 500;
        color: $text-color-price;
      }
    }
  }
}
</style>
